home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / tparam.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  9KB  |  349 lines

  1. /* Merge parameters into a termcap entry string.
  2.    Copyright (C) 1985, 1987, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Emacs config.h may rename various library functions such as malloc.  */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21.  
  22. #ifdef emacs
  23. #include "lisp.h"
  24. #ifdef USE_PROTOTYPES
  25. #include <stdarg.h>
  26. #endif
  27. #include "tparam_p.h"
  28. static char * tparam1 _P_((char *string, char *outstring, int len,
  29.                            char *up, char *left, register int *argp));
  30. #endif
  31.  
  32. #else /* not HAVE_CONFIG_H */
  33.  
  34. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  35. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  36. #endif
  37.  
  38. #ifdef STDC_HEADERS
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #else
  42. char *malloc ();
  43. char *realloc ();
  44. #endif
  45.  
  46. #endif /* not HAVE_CONFIG_H */
  47.  
  48. #ifndef NULL
  49. #define NULL (char *) 0
  50. #endif
  51.  
  52. #ifndef emacs
  53. static void
  54. memory_out ()
  55. {
  56.   write (2, "virtual memory exhausted\n", 25);
  57.   exit (1);
  58. }
  59.  
  60. static char *
  61. xmalloc (size)
  62.      unsigned size;
  63. {
  64.   register char *tem = malloc (size);
  65.  
  66.   if (!tem)
  67.     memory_out ();
  68.   return tem;
  69. }
  70.  
  71. static char *
  72. xrealloc (ptr, size)
  73.      char *ptr;
  74.      unsigned size;
  75. {
  76.   register char *tem = realloc (ptr, size);
  77.  
  78.   if (!tem)
  79.     memory_out ();
  80.   return tem;
  81. }
  82. #endif /* not emacs */
  83.  
  84. /* Assuming STRING is the value of a termcap string entry
  85.    containing `%' constructs to expand parameters,
  86.    merge in parameter values and store result in block OUTSTRING points to.
  87.    LEN is the length of OUTSTRING.  If more space is needed,
  88.    a block is allocated with `malloc'.
  89.  
  90.    The value returned is the address of the resulting string.
  91.    This may be OUTSTRING or may be the address of a block got with `malloc'.
  92.    In the latter case, the caller must free the block.
  93.  
  94.    The fourth and following args to tparam serve as the parameter values.  */
  95.  
  96. /* VARARGS 2 */
  97. char * _VARARGS_
  98. #ifdef USE_PROTOTYPES
  99. tparam (char *string, char *outstring, int len, ...)
  100. #else
  101. tparam (string, outstring, len, arg0, arg1, arg2, arg3)
  102.      char *string;
  103.      char *outstring;
  104.      int len;
  105.      int arg0, arg1, arg2, arg3;
  106. #endif
  107. {
  108. #ifdef USE_PROTOTYPES
  109.     va_list args;
  110.     int arg[4];
  111.     va_start(args, len);
  112.     arg[0] = va_arg(args, int);
  113.     arg[1] = va_arg(args, int);
  114.     arg[2] = va_arg(args, int);
  115.     arg[3] = va_arg(args, int);
  116.     va_end(args);
  117.     return tparam1 (string, outstring, len, NULL, NULL, arg);
  118. #elif NO_ARG_ARRAY
  119.   int arg[4];
  120.   arg[0] = arg0;
  121.   arg[1] = arg1;
  122.   arg[2] = arg2;
  123.   arg[3] = arg3;
  124.   return tparam1 (string, outstring, len, NULL, NULL, arg);
  125. #else
  126.   return tparam1 (string, outstring, len, NULL, NULL, &arg0);
  127. #endif
  128. }
  129.  
  130. char *BC;
  131. char *UP;
  132.  
  133. static char tgoto_buf[50];
  134.  
  135. char *
  136. tgoto (cm, hpos, vpos)
  137.      char *cm;
  138.      int hpos, vpos;
  139. {
  140.   int args[2];
  141.   if (!cm)
  142.     return NULL;
  143.   args[0] = vpos;
  144.   args[1] = hpos;
  145.   return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
  146. }
  147.  
  148. static char *
  149. tparam1 (string, outstring, len, up, left, argp)
  150.      char *string;
  151.      char *outstring;
  152.      int len;
  153.      char *up, *left;
  154.      register int *argp;
  155. {
  156.   register int c;
  157.   register char *p = string;
  158.   register char *op = outstring;
  159.   char *outend;
  160.   int outlen = 0;
  161.  
  162.   register int tem;
  163.   int *old_argp = argp;
  164.   int doleft = 0;
  165.   int doup = 0;
  166.  
  167.   outend = outstring + len;
  168.  
  169.   while (1)
  170.     {
  171.       /* If the buffer might be too short, make it bigger.  */
  172.       if (op + 5 >= outend)
  173.     {
  174.       register char *new;
  175.       if (outlen == 0)
  176.         {
  177.           outlen = len + 40;
  178.           new = (char *) xmalloc (outlen);
  179.           outend += 40;
  180.           bcopy (outstring, new, op - outstring);
  181.         }
  182.       else
  183.         {
  184.           outend += outlen;
  185.           outlen *= 2;
  186.           new = (char *) xrealloc (outstring, outlen);
  187.         }
  188.       op += new - outstring;
  189.       outend += new - outstring;
  190.       outstring = new;
  191.     }
  192.       c = *p++;
  193.       if (!c)
  194.     break;
  195.       if (c == '%')
  196.     {
  197.       c = *p++;
  198.       tem = *argp;
  199.       switch (c)
  200.         {
  201.         case 'd':        /* %d means output in decimal.  */
  202.           if (tem < 10)
  203.         goto onedigit;
  204.           if (tem < 100)
  205.         goto twodigit;
  206.         case '3':        /* %3 means output in decimal, 3 digits.  */
  207.           if (tem > 999)
  208.         {
  209.           *op++ = tem / 1000 + '0';
  210.           tem %= 1000;
  211.         }
  212.           *op++ = tem / 100 + '0';
  213.         case '2':        /* %2 means output in decimal, 2 digits.  */
  214.         twodigit:
  215.           tem %= 100;
  216.           *op++ = tem / 10 + '0';
  217.         onedigit:
  218.           *op++ = tem % 10 + '0';
  219.           argp++;
  220.           break;
  221.  
  222.         case 'C':
  223.           /* For c-100: print quotient of value by 96, if nonzero,
  224.          then do like %+.  */
  225.           if (tem >= 96)
  226.         {
  227.           *op++ = tem / 96;
  228.           tem %= 96;
  229.         }
  230.         case '+':        /* %+x means add character code of char x.  */
  231.           tem += *p++;
  232.         case '.':        /* %. means output as character.  */
  233.           if (left)
  234.         {
  235.           /* If want to forbid output of 0 and \n and \t,
  236.              and this is one of them, increment it.  */
  237.           while (tem == 0 || tem == '\n' || tem == '\t')
  238.             {
  239.               tem++;
  240.               if (argp == old_argp)
  241.             doup++, outend -= strlen (up);
  242.               else
  243.             doleft++, outend -= strlen (left);
  244.             }
  245.         }
  246.           *op++ = tem ? tem : 0200;
  247.         case 'f':        /* %f means discard next arg.  */
  248.           argp++;
  249.           break;
  250.  
  251.         case 'b':        /* %b means back up one arg (and re-use it).  */
  252.           argp--;
  253.           break;
  254.  
  255.         case 'r':        /* %r means interchange following two args.  */
  256.           argp[0] = argp[1];
  257.           argp[1] = tem;
  258.           old_argp++;
  259.           break;
  260.  
  261.         case '>':        /* %>xy means if arg is > char code of x, */
  262.           if (argp[0] > *p++) /* then add char code of y to the arg, */
  263.         argp[0] += *p;    /* and in any case don't output.  */
  264.           p++;        /* Leave the arg to be output later.  */
  265.           break;
  266.  
  267.         case 'a':        /* %a means arithmetic.  */
  268.           /* Next character says what operation.
  269.          Add or subtract either a constant or some other arg.  */
  270.           /* First following character is + to add or - to subtract
  271.          or = to assign.  */
  272.           /* Next following char is 'p' and an arg spec
  273.          (0100 plus position of that arg relative to this one)
  274.          or 'c' and a constant stored in a character.  */
  275.           tem = p[2] & 0177;
  276.           if (p[1] == 'p')
  277.         tem = argp[tem - 0100];
  278.           if (p[0] == '-')
  279.         argp[0] -= tem;
  280.           else if (p[0] == '+')
  281.         argp[0] += tem;
  282.           else if (p[0] == '*')
  283.         argp[0] *= tem;
  284.           else if (p[0] == '/')
  285.         argp[0] /= tem;
  286.           else
  287.         argp[0] = tem;
  288.  
  289.           p += 3;
  290.           break;
  291.  
  292.         case 'i':        /* %i means add one to arg, */
  293.           argp[0] ++;    /* and leave it to be output later.  */
  294.           argp[1] ++;    /* Increment the following arg, too!  */
  295.           break;
  296.  
  297.         case '%':        /* %% means output %; no arg.  */
  298.           goto ordinary;
  299.  
  300.         case 'n':        /* %n means xor each of next two args with 140.  */
  301.           argp[0] ^= 0140;
  302.           argp[1] ^= 0140;
  303.           break;
  304.  
  305.         case 'm':        /* %m means xor each of next two args with 177.  */
  306.           argp[0] ^= 0177;
  307.           argp[1] ^= 0177;
  308.           break;
  309.  
  310.         case 'B':        /* %B means express arg as BCD char code.  */
  311.           argp[0] += 6 * (tem / 10);
  312.           break;
  313.  
  314.         case 'D':        /* %D means weird Delta Data transformation.  */
  315.           argp[0] -= 2 * (tem % 16);
  316.           break;
  317.         }
  318.     }
  319.       else
  320.     /* Ordinary character in the argument string.  */
  321.       ordinary:
  322.     *op++ = c;
  323.     }
  324.   *op = 0;
  325.   while (doup-- > 0)
  326.     strcat (op, up);
  327.   while (doleft-- > 0)
  328.     strcat (op, left);
  329.   return outstring;
  330. }
  331.  
  332. #ifdef DEBUG
  333.  
  334. main (argc, argv)
  335.      int argc;
  336.      char **argv;
  337. {
  338.   char buf[50];
  339.   int args[3];
  340.   args[0] = atoi (argv[2]);
  341.   args[1] = atoi (argv[3]);
  342.   args[2] = atoi (argv[4]);
  343.   tparam1 (argv[1], buf, "LEFT", "UP", args);
  344.   printf ("%s\n", buf);
  345.   return 0;
  346. }
  347.  
  348. #endif /* DEBUG */
  349.